package service

import (
	"errors"
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"
	"tuowei.com/app/api/models"
	"tuowei.com/app/api/service/dto"
	"tuowei.com/common/actions"
	cDto "tuowei.com/common/dto"
)

type ApiIndex struct {
	service.Service
}

func (e *ApiIndex) GetPageWZ(c *dto.ApiConsultWZGetPageReq, p *actions.DataPermission, list *[]models.SysConsultWZ, count *int64) error {
	var err error
	var data models.SysConsultWZ

	err = e.Orm.Debug().
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).Table(data.TableName()).Where("deleted_at is null").Order("wz_hot,consult_wz_id desc").
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}
func (e *ApiIndex) Get(d *dto.ApiConsultWZGetReq, p *actions.DataPermission, model *models.SysConsultWZ) error {
	var data models.SysConsultWZ

	err := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error

	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("db error: %s", err)
		return err
	}

	return nil
}

func (e *ApiIndex) GetPageBoutiqueCourse(c *dto.ApiCourseBoutiqueGetPageReq, p *actions.DataPermission, list *[]models.SysCourseByBoutique, count *int64) error {
	var err error
	var data models.SysCourseByBoutique

	err = e.Orm.Debug().
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).Table(data.TableName()).Where("is_boutique = 1 and course_type = 'online'").
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}

func (e *ApiIndex) WzAddNum(c *dto.SysConsultWZUpdateReq) error {
	var err error
	var model = models.SysConsultWZ{}
	e.Orm.First(&model, c.GetId())
	c.Generate(&model)
	db := e.Orm.Save(&model)
	err = db.Error
	if err != nil {
		e.Log.Errorf("Service Update SysConsultWZUpdateReq error:%s", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")

	}
	return nil
}

func (e *ApiIndex) GetQuestionBankWare(qb models.SysExercises, data *[]models.SysQuestionBankWare) error {
	var err error
	err = e.Orm.Debug().Table("sys_question_bank_ware").Where("question_bank_id= ?", qb.Id).Find(data).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}

// GetPageAcademy 获取推荐学院列表
func (e *ApiIndex) GetPageAcademy(c *dto.ApiAcademyGetPageReq, p *actions.DataPermission, list *[]models.SysAcademy, count *int64) error {
	var err error
	var data models.SysAcademy

	err = e.Orm.Debug().
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).Table(data.TableName()).Order("academy_hot,academy_id desc").
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}

// GetAcademy 根据id获取推荐学院文章
func (e *ApiIndex) GetAcademy(d *dto.ApiAcademyGetReq, p *actions.DataPermission, model *models.SysAcademy) error {
	var data models.SysAcademy

	err := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error

	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("db error: %s", err)
		return err
	}

	return nil
}
